package cn.edu.ctbu.controller.admin;

import cn.edu.ctbu.DTO.RejectOrdersDTO;
import cn.edu.ctbu.VO.OrderDetailVO;
import cn.edu.ctbu.VO.OrdersVO;
import cn.edu.ctbu.entity.Orders;
import cn.edu.ctbu.result.Result;
import cn.edu.ctbu.service.IOrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/admin/orders")
@Api(tags = "订单相关接口")
public class OrdersController {

    @Autowired
    IOrdersService iOrdersService;

    /**
     * 获得订单列表
     */
    @ApiOperation("获得订单列表")
    @GetMapping
    public Result<List<OrdersVO>> get(String number, String phone, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startTime, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime){
        List<OrdersVO> list=iOrdersService.get(number,phone,startTime,endTime);
        return Result.success(list);
    }

    /**
     * 根据时间获得订单
     */
    @ApiOperation("根据某一天时间获得订单列表")
    @GetMapping("/day")
    public Result<List<Orders>> day(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate time){
        List<Orders> list= iOrdersService.day(time);
        return Result.success(list);
    }

    /**
     * 获得订单详情
     */
    @ApiOperation("获得订单详情")
    @GetMapping("/detail")
    public Result<OrderDetailVO> detail(Long id){
       OrderDetailVO vo= iOrdersService.detail(id);
       return Result.success(vo);
    }
    /**
     * 根据订单号获得订单详情
     */
    @ApiOperation("根据订单号获得订单详情")
    @GetMapping("/detail/number")
    public Result<OrderDetailVO> detail(String number){
        OrderDetailVO vo= iOrdersService.detailByNumber(number);
        return Result.success(vo);
    }

    /**
     * 接单
     */
    @ApiOperation("接单")
    @PutMapping("/receive/{id}")
    public Result receive(@PathVariable Long id){
        iOrdersService.receive(id);
        return Result.success();
    }

    /**
     * 拒单
     */
    @ApiOperation("拒单")
    @PutMapping("/reject")
    public Result reject(@RequestBody RejectOrdersDTO dto){
        iOrdersService.reject(dto);
        return Result.success();
    }
    /**
     * 取消订单
     */
    @ApiOperation("取消订单")
    @PutMapping("/cancel")
    public Result cancel(@RequestBody RejectOrdersDTO dto){
        iOrdersService.cancel(dto);
        return Result.success();
    }

    /**
     * 派送订单
     */
    @ApiOperation("派送订单")
    @PutMapping("/delivery/{id}")
    public Result delivery(@PathVariable Long id){
        iOrdersService.delivery(id);
        return Result.success();
    }

    /**
     * 完成订单
     */
    @ApiOperation("完成订单")
    @PutMapping("/complete/{id}")
    public Result complete(@PathVariable Long id){
        iOrdersService.complete(id);
        return Result.success();
    }

    /**
     * 删除订单
     */
    @ApiModelProperty("删除订单")
    @DeleteMapping("/delete")
    public Result delete(Long id){
        iOrdersService.delete(id);
        return Result.success();
    }
}
